# The class Reporting, defines the place to report the test result.

require 'win32ole'
class Reporting
    @@passed_tests = 0
    @@failed_tests = 0
    @@notes=0
    @@current_test_case = ""
  
    def initialize(dir,filename)
        @@excel = WIN32OLE.new("excel.application")
        WIN32OLE.const_load(@@excel)
        @@excel.visible = false
        @@workbook = @@excel.workbooks.add
        @@worksheet = @@workbook.Worksheets(1)
        @@worksheet.range("a1:h600").Interior['ColorIndex'] = 2
        @@worksheet.range("a1:c1")['Value'] = ["Tests", "#", "%"]
        @@worksheet.range("a7:h7")['Value'] = ["Test #", "Result", "Test Case", "Name", "Error Log", "Error Page", "Expected result", "Actual result"]
        @@worksheet.range("b1:c1, a7").HorizontalAlignment = WIN32OLE::XlRight
        @@worksheet.range("a1:c1, a7:h7").Interior['ColorIndex'] = 11
        @@worksheet.range("a4:c4").Interior['ColorIndex'] = 36
        @@worksheet.range("a1:c1, a7:h7").Font['ColorIndex'] = 2
        @@worksheet.range("a2:c2").Font['ColorIndex'] = 10 
        @@worksheet.range("a3:c3").Font['ColorIndex'] = 3
        @@worksheet.range("a4:c4").Font['ColorIndex'] = 5        
        @@worksheet.range("a1:c1, a5:c5, a7:h7").Font.Bold = true
        @@worksheet.range("a2:c2")['Value'] = ["Passed", @@passed_tests, "=b2/b4"]
        @@worksheet.range("a3:c3")['Value'] = ["Failed", @@failed_tests, "=b3/b4"]
        @@worksheet.range("a4:c4")['Value'] = ["Total", "=b2+b3", "=c2+c3"]  
        @@worksheet.range("c2, c3, c4").NumberFormat = "0.00%"
        @@worksheet.columns("a:h").AutoFit
        
        @@workbook.saveas(dir + "TestReport_#{filename}_"+Time.now.strftime("%Y%m%d_%H%M")+".xls")
    end
    
    # If boolean=true, put the test_name as passed into report file
    # If boolean=false, put the test_name as failed into report file
    def test_validate(boolean, test_name)
        if boolean==true
            return test_passed(test_name)
        else
            return test_failed(test_name)
        end
    end
    
    def test_equal(expected, actual, test_name)
        if expected.eql?(actual)
            return test_passed(test_name)
        else
            expected = expected.inspect
            actual = actual.inspect
            return test_failed(test_name, expected, actual)
        end
    end
    
    def test_include(expected, actual, test_name)
        if actual.include?(expected)
            return test_passed(test_name)
        else
            expected = expected.inspect
            actual = actual.inspect
            return test_failed(test_name, expected, actual)
        end
    end
    
    # Put the passed test_name into the log file.
    def test_passed(test_name)
        @@passed_tests += 1
        @@worksheet.range("b2").Value = @@passed_tests
        @@worksheet.range("b"+ (7+total_tests()).to_s).Font['ColorIndex'] = 10 
        @@worksheet.range("b"+ (7+total_tests()).to_s).Font.Bold = true    
        @@worksheet.range("a"+ (7+total_tests()).to_s + ":d" + (7+total_tests()).to_s).Value = [total_tests(), "Passed", @@current_test_case, test_name]
        save()
    end
  
    # Put the failed test_name into the log file.
    def test_failed(test_name, expected="", actual="")
        @@failed_tests += 1
        @@worksheet.range("b3").Value = @@failed_tests
        @@worksheet.range("b"+ (7+total_tests()).to_s).Font['ColorIndex'] = 3
        @@worksheet.range("h"+ (7+total_tests()).to_s).Font['ColorIndex'] = 3
        @@worksheet.range("b"+ (7+total_tests()).to_s).Font.Bold = true
        @@worksheet.range("a"+ (7+total_tests()).to_s + ":h" + (7+total_tests()).to_s).Value = [total_tests(), "Failed", @@current_test_case, test_name, error_builder, $ie.url.to_s, expected, actual]        
        save()
    end
    
    def test_notes(notes_info)
        @@notes+=1
        @@worksheet.range("a"+ (7+total_tests()).to_s + ":d" + (7+total_tests()).to_s).Font['ColorIndex'] = 5
        @@worksheet.range("a"+ (7+total_tests()).to_s + ":d" + (7+total_tests()).to_s).Font.Bold = true
        @@worksheet.range("a"+ (7+total_tests()).to_s + ":d" + (7+total_tests()).to_s).Value= [total_tests(), "Notes", @@current_test_case, notes_info]
        save()
    end
    
    def total_tests()
        return @@passed_tests + @@failed_tests+ @@notes
    end
    
    def error_builder   
        if $!==nil
            return ""
        else 
            str= $!.to_s
        end 

        if $@.length==1
            return "#{str}\n#{$@.first}"
        else
            return "#{str}\n#{$@.first}\n#{$@.last}"
        end
    end
  
    def border()  
        test_total = 7+total_tests()
        visited_begin = 3+test_total 
        visited_end = visited_begin+$ie.url_list.length
    
        @@worksheet.range("a1:c4").Borders['ColorIndex'] = WIN32OLE::XlAutomatic
        @@worksheet.range("a1:c4").Borders['LineStyle'] = WIN32OLE::XlEdgeRight 
        @@worksheet.range("a1:c4").Borders['LineStyle'] = WIN32OLE::XlEdgeTop
        @@worksheet.range("a1:c4").Borders['LineStyle'] = WIN32OLE::XlEdgeBottom
        @@worksheet.range("a1:c4").Borders['LineStyle'] = WIN32OLE::XlEdgeLeft
        @@worksheet.range("a1:c4").Borders['Weight'] = WIN32OLE::XlMedium
        @@worksheet.range("a1:c4").Borders['LineStyle'] = WIN32OLE::XlContinuous       
        @@worksheet.range("a1:c4").Borders(WIN32OLE::XlInsideVertical).LineStyle = WIN32OLE::XlNone
        @@worksheet.range("a1:c4").Borders(WIN32OLE::XlInsideHorizontal).LineStyle = WIN32OLE::XlNone  
    
        @@worksheet.range("a7:h#{test_total}").Borders['ColorIndex'] = WIN32OLE::XlAutomatic
        @@worksheet.range("a7:h#{test_total}").Borders['LineStyle'] = WIN32OLE::XlEdgeRight 
        @@worksheet.range("a7:h#{test_total}").Borders['LineStyle'] = WIN32OLE::XlEdgeTop
        @@worksheet.range("a7:h#{test_total}").Borders['LineStyle'] = WIN32OLE::XlEdgeBottom
        @@worksheet.range("a7:h#{test_total}").Borders['LineStyle'] = WIN32OLE::XlEdgeLeft
        @@worksheet.range("a7:h#{test_total}").Borders['Weight'] = WIN32OLE::XlMedium
        @@worksheet.range("a7:h#{test_total}").Borders['LineStyle'] = WIN32OLE::XlContinuous        
        @@worksheet.range("a7:h#{test_total}").Borders(WIN32OLE::XlInsideVertical).LineStyle = WIN32OLE::XlNone
        @@worksheet.range("a7:h#{test_total}").Borders(WIN32OLE::XlInsideHorizontal).LineStyle = WIN32OLE::XlNone
    
        @@worksheet.range("h#{visited_begin}:h#{visited_end}").Borders['ColorIndex'] = WIN32OLE::XlAutomatic
        @@worksheet.range("h#{visited_begin}:h#{visited_end}").Borders['LineStyle'] = WIN32OLE::XlEdgeRight 
        @@worksheet.range("h#{visited_begin}:h#{visited_end}").Borders['LineStyle'] = WIN32OLE::XlEdgeTop
        @@worksheet.range("h#{visited_begin}:h#{visited_end}").Borders['LineStyle'] = WIN32OLE::XlEdgeBottom
        @@worksheet.range("h#{visited_begin}:h#{visited_end}").Borders['LineStyle'] = WIN32OLE::XlEdgeLeft
        @@worksheet.range("h#{visited_begin}:h#{visited_end}").Borders['Weight'] = WIN32OLE::XlMedium
        @@worksheet.range("h#{visited_begin}:h#{visited_end}").Borders['LineStyle'] = WIN32OLE::XlContinuous        
        @@worksheet.range("h#{visited_begin}:h#{visited_end}").Borders(WIN32OLE::XlInsideVertical).LineStyle = WIN32OLE::XlNone
        @@worksheet.range("h#{visited_begin}:h#{visited_end}").Borders(WIN32OLE::XlInsideHorizontal).LineStyle = WIN32OLE::XlNone   
    
        @@worksheet.range("a#{visited_begin}:c#{visited_begin+3}").Borders['ColorIndex'] = WIN32OLE::XlAutomatic
        @@worksheet.range("a#{visited_begin}:c#{visited_begin+3}").Borders['LineStyle'] = WIN32OLE::XlEdgeRight 
        @@worksheet.range("a#{visited_begin}:c#{visited_begin+3}").Borders['LineStyle'] = WIN32OLE::XlEdgeTop
        @@worksheet.range("a#{visited_begin}:c#{visited_begin+3}").Borders['LineStyle'] = WIN32OLE::XlEdgeBottom
        @@worksheet.range("a#{visited_begin}:c#{visited_begin+3}").Borders['LineStyle'] = WIN32OLE::XlEdgeLeft
        @@worksheet.range("a#{visited_begin}:c#{visited_begin+3}").Borders['Weight'] = WIN32OLE::XlMedium
        @@worksheet.range("a#{visited_begin}:c#{visited_begin+3}").Borders['LineStyle'] = WIN32OLE::XlContinuous        
        @@worksheet.range("a#{visited_begin}:c#{visited_begin+3}").Borders(WIN32OLE::XlInsideVertical).LineStyle = WIN32OLE::XlNone
        @@worksheet.range("a#{visited_begin}:c#{visited_begin+3}").Borders(WIN32OLE::XlInsideHorizontal).LineStyle = WIN32OLE::XlNone
    end
  
    def effectiveness()
        @@worksheet.range("a" + (10+total_tests()).to_s + ":c" + (10+total_tests()).to_s)['Value'] = ["Effectiveness", "", "Total"]
        @@worksheet.range("c" + (10+total_tests()).to_s).HorizontalAlignment = WIN32OLE::XlRight
        @@worksheet.range("a" + (10+total_tests()).to_s + ":c" + (10+total_tests()).to_s).Interior['ColorIndex'] = 11  
        @@worksheet.range("a" + (10+total_tests()).to_s + ":c" + (10+total_tests()).to_s).Font['ColorIndex'] = 2    
        @@worksheet.range("a" + (10+total_tests()).to_s + ":c" + (10+total_tests()).to_s + ", a" + (13+total_tests()).to_s + ":c" + (13+total_tests()).to_s).Font.Bold = true
        @@worksheet.range("a" + (11+total_tests()).to_s + ":c" + (11+total_tests()).to_s)['Value'] = ["Pages Accessed", "", "#{$ie.url_list.length-2}"]
        @@worksheet.range("a" + (12+total_tests()).to_s + ":c" + (12+total_tests()).to_s)['Value'] = ["Pages Total", "", "254"]
        @@worksheet.range("a" + (13+total_tests()).to_s + ":c" + (13+total_tests()).to_s)['Value'] = ["Percentage", "", "=c" + (11+total_tests()).to_s + "/ c" + (12+total_tests()).to_s]
        @@worksheet.range("a" + (10+total_tests()).to_s + ":b" + (10+total_tests()).to_s).merge
        @@worksheet.range("a" + (11+total_tests()).to_s + ":b" + (11+total_tests()).to_s).merge
        @@worksheet.range("a" + (12+total_tests()).to_s + ":b" + (12+total_tests()).to_s).merge
        @@worksheet.range("a" + (13+total_tests()).to_s + ":b" + (13+total_tests()).to_s).merge
        @@worksheet.range("a" + (13+total_tests()).to_s + ":c" + (13+total_tests()).to_s).Interior['ColorIndex'] = 36 
        @@worksheet.range("c" + (13+total_tests()).to_s).NumberFormat = "0.00%"
    end
  
    def pages_visited()
        url_cleanup()
        i = 0
        @@worksheet.range("d"+ (10+total_tests()).to_s)['Value'] = ["Pages Visited"]
        @@worksheet.range("d"+ (10+total_tests()).to_s).HorizontalAlignment = WIN32OLE::XlCenter
        @@worksheet.range("d"+ (10+total_tests()).to_s).Interior['ColorIndex'] = 11  
        @@worksheet.range("d"+ (10+total_tests()).to_s).Font['ColorIndex'] = 2    
        @@worksheet.range("d"+ (10+total_tests()).to_s).Font.Bold = true
        @@urls.each do |page|
            @@worksheet.range("d"+ (i+11+total_tests()).to_s).Value = [page]
            i += 1
        end
    end
  
    def url_cleanup()
        i=0
        a = $ie.url_list
        a.delete_if{|d| d == "about:blank"}
        a.delete_if{|d| d == "javascript:false;"}
        a.delete_if{|d| d == "javascript:'<html></html>'"}
        a.each{ |page|
            a[i] = page.split("?")[0]          
            i=i+1}       
        @@urls = a.uniq()
    end
  
    def set_case(name)
        @@current_test_case = name.to_s
    end
  
    def save()
        @@workbook.Save()
    end
  
    def close()
        pages_visited()
        effectiveness()
        @@worksheet.range("a7:h#{7+total_tests()}").VerticalAlignment = WIN32OLE::XlCenter
        @@worksheet.Columns("E:E").ColumnWidth = 65
        @@worksheet.columns("a:h").AutoFit
        border()
        save()
        @@workbook.close
        @@excel.Quit
    end
end